﻿define([
    'modules/ApiClient',
    "jquery",
    'jquery.cookie'
], function (ApiClient,$) {
    var Selection_BusSystem = function (obj_id) {
        var id_Array = obj_id.split('-');
        var SysClient = id_Array[1] + '_client';
        //ApiClient.get('url', '');
        ApiClient.get('client/' + SysClient).then(function (reValues) {
            //alert(reValues.data.code);
            if (reValues.data.code == "200") {
                openNewWindow(reValues.data.reMessage.client_url);
                let cookieValue = $.cookie('UserToken') || ''; // 获得cookie
                $.cookie('UserToken', cookieValue, { expires: 7, path: '/', domain: reValues.data.reMessage.client_url });//新建一个cookie 包括有效期 路径 域名等  
            } else if (reValues.data.code == "401") {
                openNewPage(reValues.data.reMessage);
            } else {
                openNewPage(reValues.data.reMessage);
            }
        });
        //alert(SysClient);
    };
    var openNewWindow = function(url) {
        //var a = $('a')[0];
        let a = $("<a href='" + url+"' target='_blank'>baidu</a>").get(0);
        let e = document.createEvent('MouseEvents');
        e.initEvent('click', true, true);
        a.dispatchEvent(e);
    };
    var openNewPage = function (url) {
        //var a = $('a')[0];
        let a = $("<a href='" + url + "'>baidu</a>").get(0);
        let e = document.createEvent('MouseEvents');
        e.initEvent('click', true, true);
        a.dispatchEvent(e);
    };
    return {
        Selection_BusSystem: Selection_BusSystem
    };
});